windowloads()
getname ()

document.onselectstart = function() {return false};
document.unselectable = 'on';   

function outlogin(){ outloginfn()}
   var username = getCookie('username')
   if(!username){
    layer.msg('请先登录',{
        icon:2,
        time:2000
    },function(){
        localStorage.setItem('url',location.href)
        location.href = '/login.html'
    })
   }
   var userid = getCookie('userid')

   var divbox = $('.centers .center>div')
   
function getdataall(){
  $.ajax({
    type: 'get',
    url: 'cart/api/list/'+userid,
    headers: {
        'Authorization': localStorage.getItem('token')
    },
    success: data=> {
        if(!data.data){
              $('.content')[0].innerHTML = `
              <div class="jumbotron">
                <img src="imges/cart-empty.png" alt="">
                <div class="box">
                    <h1>您的购物车是空的！</h1>
                    <p><a class="btn btn-primary btn-lg butnn" href="list.html" role="button">马上去购物</a></p>
                </div>
            </div>
              `  
        }else{
            var html = `
            <div class="type_areas">
            <p class="nveslist">您当前的位置：<a href="index.html">名鞋库</a>/<a href="list.html">列表页</a>/<a id="active">购物车</a>
            </p>
            <hr>

            <h1>全部商品<span>${data.data.length}</span></h1>
            <div class="settlement">

                <div class="top">
                    <div><input type="checkbox" class="checkall">全选</div>
                    <div>商品名称</div>
                    <div>单价</div>
                    <div>数量</div>
                    <div>小计</div>
                    <div>操作</div>
                </div>
                <div class="centers">`

    var goodsid = data.data.map(item=>item.goodsid)

    var number = parseInt(Math.random()*3000+1000)
    for(var i = 0;i<goodsid.length;i++){
        $.ajax({
            type: 'get',
            url: 'goods/api/detail/'+goodsid[i],
            async:false,
            success: res=> {
                html += `
                <div class="center">
                    <div data-id="${data.data[i].goodsid}"><input type="checkbox" class="checkone"></div>
                    <div><a href="detail.html?id=${data.data[i].goodsid}"><img src = "${res.data.goodsdata.goodsimg1}"/></a></div>
                    <div> <a href="detail.html?id=${data.data[i].goodsid}">${res.data.goodsdata.goodsname}</a></div>
                    <div class="price"><span>${number}</span></div>
                    <div data-id="${data.data[i].goodsid}">
                        <i class="dell" onclick="del()">-</i>
                        <input type="text" value="${data.data[i].goodsnumber}" class="textnum">
                        <i class="add" onclick="add()">+</i>
                    </div>
                    <div class="pricenum"><span>${number*data.data[i].goodsnumber}</span></div>
                    <div class="removeone" data-id="${data.data[i].goodsid}"><i>删除</i></div>
                </div>   `
            }

        })
    }
        html += `
        </div>
        <div class="bottom">
                    <div><input type="checkbox"  class="checkall">全选</div>
                    <div class="removechecked"><a href="javascript:void(0);">删除选中的商品</a></div>
                    <div>移入收藏</div>
                    <div></div>
                    <div>已选<span class ="checkedspi">0</span>件商品</div>
                    <div>合计：<span class = "allprice">0</span>元</div>
                    <div> <span class = "settlement">结算</span></div>
                </div>
            </div>
        </div>
        `
        $('.content')[0].innerHTML = html
        deladd()
        removeone()
        removechecked()
        total()
    }
    }
})
}

getdataall()

function deladd(){
    var delbtn = document.querySelectorAll('.dell')
    var addbtn = document.querySelectorAll('.add')

    var goodsid
    addbtn.forEach(btn=>{
        btn.onclick = function(){
            goodsid = this.parentElement.dataset.id
            var num =  +this.previousElementSibling.value
            if(num>=100){
                return 
            }
            
            num++
            this.previousElementSibling.value = num
            deladdAjax(goodsid,num)
            total()
        }
    })
    delbtn.forEach(btn=>{
        btn.onclick = function(){
            goodsid = this.parentElement.dataset.id
            var num =  +this.nextElementSibling.value
            if(num<=1){
                return 
            }
            num--
            this.nextElementSibling.value = num
            deladdAjax(goodsid,num)
            total()
        }
    })
}
function deladdAjax(goodsid,num){
    $.ajax({
        type: 'post',
        url:'cart/api/changnumber',
        data:{
            num,
            userid:getCookie('userid'),
            goodsid,
        },
        headers: {
            'Authorization': localStorage.getItem('token')
        }
    })
}

function removeone(){
    var removeonebtn = document.querySelectorAll('.removeone')
    removeonebtn.forEach(btn=>{
        btn.onclick = function(){
            
            var _this = this
            layer.confirm('你确定要删除吗？',{
                btn:['删除','取消']
            
        },function(){
            var goodsid = _this.dataset.id
            $.ajax({
                type: 'post',
                url:'cart/api/removeone',
                data:{
                    userid:getCookie('userid'),
                    goodsid,
                },
                headers: {
                    'Authorization': localStorage.getItem('token')// 获取本地存储的token
                },
                success:res=>{
                    _this.parentElement.parentElement.removeChild(_this.parentElement)
                    layer.msg('删除成功',{
                        icon:2,
                        time:1000
                    },function(){
                        getdataall()
                    })
                }
            })

        })
        }
    })

}

function removechecked(){
    var  delcheckedbtn= document.querySelector('.removechecked a')
    var checkeds = document.querySelectorAll('.center .checkone')
    var selectall = document.querySelectorAll('.checkall')

    checkeds = Array.from(checkeds)
    selectall.forEach(item=>{
        item.onchange = function(){
            checkeds.forEach(v =>{
                v.checked = item.checked
            })
            selectall.forEach(v =>{
                v.checked = item.checked
            })
            total()
        }
   })
   checkeds.forEach(item=>{
    item.onchange = function(){
        selectall.forEach(v =>{
            v.checked =  checkeds.every(vc => vc.checked)  
        })
        total()
    }}) 
  delcheckedbtn.onclick=function(){
    let _this = this
    let art =  Array.from( _this.parentElement.parentElement.parentElement.children[1].children) 
    let num = 0
    art.forEach(item=> {
        if(item.children[0].children[0].checked){
            num++
        }
    })
    if(!num){
        return
    }
    layer.confirm('你确定要删除选中的商品吗？',{
        btn:['确定','取消']      
        },function(){     
    art.forEach(item=> {
            if(item.children[0].children[0].checked){
                $.ajax({
                    type: 'post',
                    url:'cart/api/removechecked',
                    data:{
                        userid:getCookie('userid'),
                        goodsid:item.children[0].dataset.id
                    },
                    headers: {
                        'Authorization': localStorage.getItem('token')// 获取本地存储的token
                    },
                    success:res=>{
                        item.parentElement.removeChild(item)
                        layer.msg('删除成功',{
                            icon:1,
                            time:1000
                        },function(){
                            getdataall()
                        })
                    }
                })
            }
        }) 
        })
    }
}

function  total(){
    var  spnums= document.querySelectorAll('.textnum')
    var allprice = document.querySelector('.allprice')
    var allprices = 0
     var totalnum = 0
    spnums.forEach((input)=>{
        var price = +input.parentElement.previousElementSibling.children[0].innerHTML
        var totalprice = +input.value * price
        input.parentElement.nextElementSibling.children[0].innerHTML = totalprice
        var lodinput = input.value
        var checkeds = input.parentElement.parentElement.firstElementChild.firstElementChild.checked
        if(checkeds){
            totalnum += +input.value
            allprices += +totalprice
        }
        allprice.innerText = allprices
         document.querySelector('.checkedspi').innerText = totalnum  
        
        input.onchange = function(){
            var reg = /\D/
            if(input.value < 1){
                input.value = 1
            }else if(input.value>100){
                layer.msg('该商品最多只能购买100件',{
                    icon:2,
                    time:2000
                },function(){
                    input.value = lodinput
                })
            }else if(reg.test(input.value)){
                layer.msg('商品数量输入错误，请重新输入',{
                    icon:2,
                    time:2000
                },function(){
                    input.value = +lodinput
                })
            }else{
            var price = +input.parentElement.previousElementSibling.children[0].innerHTML
            var totalprice = +input.value * price
            input.parentElement.nextElementSibling.children[0].innerHTML=totalprice

        }
        }
    })
}